package com.marvels.approve.loan.controller;

import com.marvels.approve.loan.dto.McamLoanApplyInfoDTO;
import com.marvels.approve.loan.service.IMcamLoanApplyInfoService;
import com.marvels.approve.mqueues.dto.LoanApplyQueueDTO;
import com.marvels.approve.mqueues.service.ApproveILoanApplyQueueService;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
/**进件申请控制器
 * @author 何杰
 * 2018年4月19日
 */
@RestController
@RequestMapping("approve/loan/service")
public class McamLoanApplyInfoController {
	@Autowired
	private IMcamLoanApplyInfoService mcamLoanApplyInfoService ;
	@Autowired
	private ApproveILoanApplyQueueService loanApplyQueueService;
	/**查询待跑决策列表
	 * @author 何杰
	 * 2018年4月19日
	 * @return
	 */
	@RequestMapping("wdList")
	@ResponseBody
	public MarvelsResultDTO waitDecisionList(@RequestBody McamLoanApplyInfoDTO dto){
		try {
			List datas = mcamLoanApplyInfoService.findLoanApplys(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), datas);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().warn("marvels_error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), null);
		}
	}
	
	@RequestMapping("runDecision")
	@ResponseBody
	public MarvelsResultDTO runDecision(){
		try {
			LoanApplyQueueDTO loanApplyQueue = new LoanApplyQueueDTO();
			loanApplyQueue.setStatus(0);
			List<LoanApplyQueueDTO> loanApplyQueues = loanApplyQueueService.findLoanApplyQueueService(loanApplyQueue);
			// McamLoanApplyInfoDTO dto = new McamLoanApplyInfoDTO();
			// List<McamLoanApplyInfoDTO> applys = mcamLoanApplyInfoDao.select(dto);
			if (loanApplyQueues == null) {
				return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), null);
			}
			int size = loanApplyQueues.size();
			MarvelsLogUtil.getInstance().info("执行决策数量execute_decision_size>" + size);
			int count = 0;
			// 一下调用决策接口/方法
			for (LoanApplyQueueDTO applyQueue : loanApplyQueues) {
				// 同步数据以及跑决策
				MarvelsLogUtil.getInstance().info("同步数据以及跑决策LoanId:" + applyQueue.getLoanId());
				boolean isRun = false;
				try {
					isRun = mcamLoanApplyInfoService.initProcessData(applyQueue);
				} catch (Exception e) {
					MarvelsLogUtil.getInstance().error("同步数据以及跑决策loanId:" + applyQueue.getLoanId() + "处理失败，失败原因：", e);
				}
				if (isRun) {
					count++;
				}
			}
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), count);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().warn("marvels_error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), null);
		} 
	}
}
